﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Mobileterminal.Dtos.Yjb;
using Newtonsoft.Json;
using Sunny.UI;

namespace Mobileterminal.Forms.Yjb
{
    public partial class Devices : UIPage 
    {
        public Devices()
        {
            InitializeComponent();
            ShowConsumn();
            ShowConsumn1();
            ShowConsumn2();
            ShowConsumn3();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            ShowConsumn();
            ShowConsumn1();
            ShowConsumn2();
            ShowConsumn3();
        }

        private async Task ShowConsumn(string deviceType = null)
        {
            // 构建动态查询参数
            var queryParams = new List<string>();
            if (!string.IsNullOrEmpty(deviceType))
                queryParams.Add($"deviceType={Uri.EscapeDataString(deviceType)}");

            var url = "https://localhost:7224/api/Show/ShowSB";
            if (queryParams.Count > 0)
                url += "?" + string.Join("&", queryParams);
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<DeviceDTO>>>(json);
            if (result.Code == 200)
            {
                result.Data = result.Data.OrderByDescending(x => x.DeviceId).ToList();
                foreach (var item in result.Data)
                {
                    item.Statu = item.Status == false ? "停用" : "启用";
                }
                uiDataGridView1.DataSource = result.Data;
            }
        }

        private async Task ShowConsumn1()
        {
            var url = "https://localhost:7224/api/Show/ShowCX";
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ProgramInfoDTO>>>(json);

            if (result?.Code == 200)
            {
                uiDataGridView2.DataSource = new BindingList<ProgramInfoDTO>(result.Data);
                UIMessageTip.ShowOk($"成功加载 {result.Data.Count} 条数据");
            }
        }

        private async Task ShowConsumn2()
        {
            var url = "https://localhost:7224/api/Show/ShowJC";
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ResultItemInfoDTO>>>(json);

            if (result?.Code == 200)
            {
                uiDataGridView3.DataSource = new BindingList<ResultItemInfoDTO>(result.Data);
                UIMessageTip.ShowOk($"成功加载 {result.Data.Count} 条数据");
            }
        }

        private async Task ShowConsumn3()
        {
            var url = "https://localhost:7224/api/Show/ShowXSZ";
            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ResultItemInfosDTO>>>(json);

            if (result?.Code == 200)
            {
                uiDataGridView4.DataSource = new BindingList<ResultItemInfosDTO>(result.Data);
                UIMessageTip.ShowOk($"成功加载 {result.Data.Count} 条数据");
            }
        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private async void uiButton2_Click(object sender, EventArgs e)
        {
            // 获取输入条件
            string deviceType = uiTextBox1.Text.Trim();

            try
            {
                await ShowConsumn(deviceType);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询异常：{ex.Message}");
            }
        }

        private async void uiTextBox1_TextChanged(object sender, EventArgs e)
        {      // 获取输入条件
            string deviceType = " ";

            try
            {
                await ShowConsumn(deviceType);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询异常：{ex.Message}");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (AddDevices add = new AddDevices())
            {
                if (add.ShowDialog() == DialogResult.OK)
                {
                    Refresh(); // 操作成功后刷新
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (AddProgramInfo add = new AddProgramInfo())
            {
                if (add.ShowDialog() == DialogResult.OK)
                {
                    Refresh(); // 操作成功后刷新
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (AddResultItemInfo add = new AddResultItemInfo())
            {
                if (add.ShowDialog() == DialogResult.OK)
                {
                    Refresh(); // 操作成功后刷新
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (AddResultItemInfos add = new AddResultItemInfos())
            {
                if (add.ShowDialog() == DialogResult.OK)
                {
                    Refresh(); // 操作成功后刷新
                }
            }
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //设备
        #region 操作 修改（uiDataGridView1）
        private void uiDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.uiDataGridView1.Columns[e.ColumnIndex].HeaderText == "操作" &&
                    this.uiDataGridView1.Rows[e.RowIndex].Cells["DeviceId"].Value != null)
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);

                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);

                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);

                    RectangleF rectMod = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    RectangleF rectDel = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);

                    Graphics grfx = CreateGraphics();
                    Color colorUse = Color.FromArgb(51, 136, 255);
                    Color colorPrint = Color.FromArgb(51, 136, 255);

                    e.Graphics.DrawString("编辑", myFont, new SolidBrush(colorUse), rectMod, sf);
                    e.Graphics.DrawString("删除", myFont, new SolidBrush(colorPrint), rectDel, sf);
                    e.Handled = true;
                }
            }
        }

        private async void uiDataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    Point curPosition = e.Location;
                    if (this.uiDataGridView1.Columns[e.ColumnIndex].HeaderText == "操作" &&
                        uiDataGridView1.Rows[e.RowIndex].Cells["DeviceId"].Value != null)
                    {
                        Graphics g = this.uiDataGridView1.CreateGraphics();
                        System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                        SizeF sizeMod = g.MeasureString("编辑", myFont);
                        SizeF sizeDel = g.MeasureString("删除", myFont);

                        Rectangle rectTotal = new Rectangle(0, 0, this.uiDataGridView1.Columns[e.ColumnIndex].Width, this.uiDataGridView1.Rows[e.RowIndex].Height);
                        RectangleF rectMod = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * (sizeMod.Width / (sizeDel.Width + sizeMod.Width)), rectTotal.Height);
                        RectangleF rectDel = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * (sizeDel.Width / (sizeDel.Width + sizeMod.Width)), rectTotal.Height);

                        if (rectMod.Contains(curPosition))
                        {
                            var DeviceId = this.uiDataGridView1.Rows[e.RowIndex].Cells["DeviceId"].Value.ToString();
                            UpdateDevices update = new UpdateDevices(DeviceId);
                            update.Show();
                        }
                        else if (rectDel.Contains(curPosition))
                        {
                            var DeviceId = this.uiDataGridView1.Rows[e.RowIndex].Cells["DeviceId"].Value.ToString();
                            var pdata = new { DeviceId = DeviceId };
                            HttpContent content = new StringContent(JsonConvert.SerializeObject(pdata), Encoding.UTF8, "application/json");
                            var hosn = await HttpClientHelper.ClientAsync("put", "https://localhost:7286/api/Yjb/DelSB", true, content);
                            DeviceDTO result = JsonConvert.DeserializeObject<DeviceDTO>(hosn);
                            if (result != null)
                            {
                                MessageBox.Show("删除成功");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作异常：{ex.Message}");
            }
        }

        private void uiDataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.uiDataGridView1.Columns[e.ColumnIndex].Name == "操作")
                {
                    this.Cursor = Cursors.Hand;
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    SetStyle(ControlStyles.DoubleBuffer, true);
                }
            }
        }
        #endregion




        //设备1
        #region 操作 修改
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 

        private void uiDataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void uiDataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.uiDataGridView2.Columns[e.ColumnIndex].HeaderText == "操作1" &&
                    this.uiDataGridView2.Rows[e.RowIndex].Cells["ProgramInfoId"].Value != null)
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);

                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);

                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);

                    RectangleF rectMod = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    RectangleF rectDel = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);

                    Graphics grfx = CreateGraphics();
                    Color colorUse = Color.FromArgb(51, 136, 255);
                    Color colorPrint = Color.FromArgb(51, 136, 255);

                    e.Graphics.DrawString("编辑", myFont, new SolidBrush(colorUse), rectMod, sf);
                    e.Graphics.DrawString("删除", myFont, new SolidBrush(colorPrint), rectDel, sf);
                    e.Handled = true;
                }
            }
        }
        private async void uiDataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                    if (this.uiDataGridView2.Columns[e.ColumnIndex].HeaderText == "操作1" && uiDataGridView2.Rows[e.RowIndex].Cells["ProgramInfoId"].Value != null)
                    {
                        Graphics g = this.uiDataGridView2.CreateGraphics();
                        System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        //bool isPrintshow = (bool)(this.uiDataGridView1.Rows[e.RowIndex].Cells["isPrintshow"].Value);
                        //var isUse = (bool)this.uiDataGridView1.Rows[e.RowIndex].Cells["isUse"].Value;
                        //var isUseStr = !isUse && isPrintshow ? "已生单" : "生单";

                        //SizeF sizeprint = g.MeasureString(isUseStr, myFont);
                        SizeF sizeMod = g.MeasureString("编辑", myFont);
                        SizeF sizeDel = g.MeasureString("删除", myFont);

                        Rectangle rectTotal = new Rectangle(0, 0, this.uiDataGridView2.Columns[e.ColumnIndex].Width, this.uiDataGridView2.Rows[e.RowIndex].Height);
                        RectangleF rectDel;
                        //RectangleF rectMod;
                        RectangleF rectMod;

                        float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                        //float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeprint.Width);
                        float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                        rectMod = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        //rectMod = new RectangleF(rectPrint.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        rectDel = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
                        //rectMod = new RectangleF(0, 0, 0, 0);

                        //判断当前鼠标在哪个“按钮”范围内
                        if (rectMod.Contains(curPosition))//编辑
                        {
                            var NumericalOrderDetail = this.uiDataGridView2.Rows[e.RowIndex].Cells["ProgramInfoId"].Value.ToString();
                            UpdateProgramInfo addshelf = new UpdateProgramInfo(NumericalOrderDetail);
                            addshelf.Show();
                            //打开新的form窗体  

                        }
                        else if (rectDel.Contains(curPosition))//删除
                        {
                            var NumericalOrderDetail = this.uiDataGridView2.Rows[e.RowIndex].Cells["ProgramInfoId"].Value.ToString();//第一种:方式请求获取dataview表中的唯一流水号ID
                            var pdata = new { ProgramInfoId = NumericalOrderDetail };
                            HttpContent content = new StringContent(JsonConvert.SerializeObject(pdata), Encoding.UTF8, "application/json");
                            var hosn = HttpClientHelper.ClientAsync("put", "https://localhost:7286/api/Yjb/DelCX", true, content).Result;
                            ConsumableMaterialDTO result = JsonConvert.DeserializeObject<ConsumableMaterialDTO>(hosn);
                            if (result != null)
                            {
                                MessageBox.Show("删除成功");
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 在按钮上鼠标箭头变成小手样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView2_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.uiDataGridView2.Columns[e.ColumnIndex].Name == "操作1")
                {
                    this.Cursor = Cursors.Hand;
                    //解决绘图时画面闪烁
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
                    SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

                }
            }
        }
        #endregion


        //设备2
        #region 操作 修改
        /// <summary>
        /// 表格的重绘事件 DataGridViewCellPaintingEventArgs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void uiDataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void uiDataGridView3_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//重绘“操作列”,使其有三个按钮
        {
            //var a=e as DataGridViewCellPaintingEventArgs;

            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                //
                if (this.uiDataGridView3.Columns[e.ColumnIndex].HeaderText == "操作2" && this.uiDataGridView3.Rows[e.RowIndex].Cells["ResultItemInfoId"].Value != null)
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置要写入字体的大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);

                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);


                    //设置每个“按钮的边界”
                    RectangleF rectMod = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    //RectangleF rectMod = new RectangleF(rectPrint.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    RectangleF rectDel = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);

                    Graphics grfx = CreateGraphics();
                    Color colorUse = Color.FromArgb(51, 136, 255);
                    Color colorPrint = Color.FromArgb(51, 136, 255);

                    e.Graphics.DrawString("编辑", myFont, new SolidBrush(colorUse), rectMod, sf);
                    e.Graphics.DrawString("删除", myFont, new SolidBrush(colorPrint), rectDel, sf); //绘制“按钮”
                    e.Handled = true;
                }
            }
        }
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiDataGridView3_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                    if (this.uiDataGridView3.Columns[e.ColumnIndex].HeaderText == "操作2" && uiDataGridView3.Rows[e.RowIndex].Cells["ResultItemInfoId"].Value != null)
                    {
                        Graphics g = this.uiDataGridView3.CreateGraphics();
                        System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        //bool isPrintshow = (bool)(this.uiDataGridView1.Rows[e.RowIndex].Cells["isPrintshow"].Value);
                        //var isUse = (bool)this.uiDataGridView1.Rows[e.RowIndex].Cells["isUse"].Value;
                        //var isUseStr = !isUse && isPrintshow ? "已生单" : "生单";

                        //SizeF sizeprint = g.MeasureString(isUseStr, myFont);
                        SizeF sizeMod = g.MeasureString("编辑", myFont);
                        SizeF sizeDel = g.MeasureString("删除", myFont);

                        Rectangle rectTotal = new Rectangle(0, 0, this.uiDataGridView3.Columns[e.ColumnIndex].Width, this.uiDataGridView3.Rows[e.RowIndex].Height);
                        RectangleF rectDel;
                        //RectangleF rectMod;
                        RectangleF rectMod;

                        float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                        //float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeprint.Width);
                        float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                        rectMod = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        //rectMod = new RectangleF(rectPrint.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        rectDel = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
                        //rectMod = new RectangleF(0, 0, 0, 0);

                        //判断当前鼠标在哪个“按钮”范围内
                        if (rectMod.Contains(curPosition))//编辑                                    
                        {
                            var NumericalOrderDetail = this.uiDataGridView3.Rows[e.RowIndex].Cells["ResultItemInfoId"].Value.ToString();
                            UpdateResultItemInfo addshelf = new UpdateResultItemInfo(NumericalOrderDetail);
                            addshelf.Show();
                            //打开新的form窗体  

                        }
                        else if (rectDel.Contains(curPosition))//删除
                        {
                            var NumericalOrderDetail = this.uiDataGridView3.Rows[e.RowIndex].Cells["ResultItemInfoId"].Value.ToString();//第一种:方式请求获取dataview表中的唯一流水号ID
                            var pdata = new { ResultItemInfoId = NumericalOrderDetail };
                            HttpContent content = new StringContent(JsonConvert.SerializeObject(pdata), Encoding.UTF8, "application/json");
                            var hosn = HttpClientHelper.ClientAsync("put", "https://localhost:7286/api/Yjb/DelJC", true, content).Result;
                            ResultItemInfoDTO result = JsonConvert.DeserializeObject<ResultItemInfoDTO>(hosn);
                            if (result != null)
                            {
                                MessageBox.Show("删除成功");
                            }

                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 在按钮上鼠标箭头变成小手样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView3_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.uiDataGridView3.Columns[e.ColumnIndex].Name == "操作2")
                {
                    this.Cursor = Cursors.Hand;
                    //解决绘图时画面闪烁
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
                    SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

                }
            }
        }
        #endregion


       

       
        

        

        

        //设备2
        #region 操作 修改
        /// <summary>
        /// 表格的重绘事件 DataGridViewCellPaintingEventArgs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void uiDataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void uiDataGridView4_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//重绘“操作列”,使其有三个按钮
        {
            //var a=e as DataGridViewCellPaintingEventArgs;

            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                //
                if (this.uiDataGridView4.Columns[e.ColumnIndex].HeaderText == "操作3" && this.uiDataGridView4.Rows[e.RowIndex].Cells["ResultItemInfosId"].Value != null) 
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式 
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置要写入字体的大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

                    SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);

                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);


                    //设置每个“按钮的边界”
                    RectangleF rectMod = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    //RectangleF rectMod = new RectangleF(rectPrint.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    RectangleF rectDel = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);

                    Graphics grfx = CreateGraphics();
                    Color colorUse = Color.FromArgb(51, 136, 255);
                    Color colorPrint = Color.FromArgb(51, 136, 255);

                    e.Graphics.DrawString("编辑", myFont, new SolidBrush(colorUse), rectMod, sf);
                    e.Graphics.DrawString("删除", myFont, new SolidBrush(colorPrint), rectDel, sf); //绘制“按钮”
                    e.Handled = true;
                }
            }
        }
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiDataGridView4_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
                {
                    Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                    if (this.uiDataGridView4.Columns[e.ColumnIndex].HeaderText == "操作3" && uiDataGridView4.Rows[e.RowIndex].Cells["ResultItemInfosId"].Value != null)
                    {
                        Graphics g = this.uiDataGridView4.CreateGraphics();
                        System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        //bool isPrintshow = (bool)(this.uiDataGridView1.Rows[e.RowIndex].Cells["isPrintshow"].Value);
                        //var isUse = (bool)this.uiDataGridView1.Rows[e.RowIndex].Cells["isUse"].Value;
                        //var isUseStr = !isUse && isPrintshow ? "已生单" : "生单";

                        //SizeF sizeprint = g.MeasureString(isUseStr, myFont);
                        SizeF sizeMod = g.MeasureString("编辑", myFont);
                        SizeF sizeDel = g.MeasureString("删除", myFont);

                        Rectangle rectTotal = new Rectangle(0, 0, this.uiDataGridView4.Columns[e.ColumnIndex].Width, this.uiDataGridView4.Rows[e.RowIndex].Height);
                        RectangleF rectDel;
                        //RectangleF rectMod;
                        RectangleF rectMod;

                        float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                        //float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeprint.Width);
                        float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                        rectMod = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        //rectMod = new RectangleF(rectPrint.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                        rectDel = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
                        //rectMod = new RectangleF(0, 0, 0, 0);

                        //判断当前鼠标在哪个“按钮”范围内
                        if (rectMod.Contains(curPosition))//编辑
                        {
                            var NumericalOrderDetail = this.uiDataGridView4.Rows[e.RowIndex].Cells["ResultItemInfosId"].Value.ToString();
                            UpdateResultItemInfos addshelf = new UpdateResultItemInfos(NumericalOrderDetail);
                            addshelf.Show();
                            //打开新的form窗体  

                        }
                        else if (rectDel.Contains(curPosition))//删除
                        {
                            var NumericalOrderDetail = this.uiDataGridView4.Rows[e.RowIndex].Cells["ResultItemInfosId"].Value.ToString();//第一种:方式请求获取dataview表中的唯一流水号ID
                            var pdata = new { ResultItemInfosId = NumericalOrderDetail };
                            HttpContent content = new StringContent(JsonConvert.SerializeObject(pdata), Encoding.UTF8, "application/json");
                            var hosn = HttpClientHelper.ClientAsync("put", "https://localhost:7286/api/Yjb/DelJG", true, content).Result;
                            ResultItemInfosDTO result = JsonConvert.DeserializeObject<ResultItemInfosDTO>(hosn);
                            if (result != null)
                            {
                                MessageBox.Show("删除成功");
                            }

                        }
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 在按钮上鼠标箭头变成小手样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView4_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.uiDataGridView4.Columns[e.ColumnIndex].Name == "操作3")
                {
                    this.Cursor = Cursors.Hand;
                    //解决绘图时画面闪烁
                    SetStyle(ControlStyles.UserPaint, true);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
                    SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

                }
            }
        }
        #endregion

        private void Devices_Initialize(object sender, EventArgs e)
        {

        }

       
    }
}